package com.espirit.eap.manager;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.espirit.eap.ValidationUtil;
import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class EntityStatusManager {
	@Resource
	GenericService genericService;
	
	@Resource
	ValidationUtil genericServiceUtil;
	
	Map<String,EntityStatus> orderStatuses = null;
	
	public EntityStatus getOrderStatus(EntityStatus orderStatus){
		return genericService.load(orderStatus);
	}
	
	public EntityStatus getRefer(){
		return getOrderStatus(EntityStatus.Refer);
	}
	public EntityStatus getAccept(){
		return getOrderStatus(EntityStatus.Accept);
	}
	
	public EntityStatus getInit(){
		return getOrderStatus(EntityStatus.Init);
	}
	
	public EntityStatus getReject(){
		return getOrderStatus(EntityStatus.Reject);
	}
	
	public EntityStatus getDeleted(){
		return getOrderStatus(EntityStatus.Deleted);
	}
	
	private EntityStatus getOrderStatus(String orderStatusCode){
		if (orderStatuses == null){
			orderStatuses = new HashMap<String, EntityStatus>();
		} 
		if (orderStatuses.get(orderStatusCode) == null){
			EntityStatus model = new EntityStatus();
			model.setCode(orderStatusCode);
			model = genericService.searchAll(model).getData(0);
			if (model != null){
				EntityStatus orderStatus = new EntityStatus();
				orderStatus.setId(model.getId());
				orderStatus.setCode(orderStatusCode);
				orderStatuses.put(orderStatusCode, orderStatus);
			} else {
				orderStatuses.put(orderStatusCode, null);
			}
		}
		return orderStatuses.get(orderStatusCode);
	}
	
	public void initData(){
		addOrderStatus(new EntityStatus(1,EntityStatus.Init,EntityStatus.Init,"初始"));
		addOrderStatus(new EntityStatus(2,EntityStatus.Refer,EntityStatus.Refer,"提交"));
		addOrderStatus(new EntityStatus(3,EntityStatus.Accept,EntityStatus.Accept,"批准"));
		addOrderStatus(new EntityStatus(4,EntityStatus.Reject,EntityStatus.Reject,"拒绝"));
		
		//addOrderStatus(new OrderStatus(5,OrderStatus.Deleted,OrderStatus.Deleted,"删除"));
	}
	
	private void addOrderStatus(EntityStatus model){
		genericServiceUtil.checkAndAdd(model);
	}
}
